﻿#ifndef TCPWRAP_H
#define TCPWRAP_H

#include <QObject>
#include <QTcpSocket>
#include <QTimer>
#include <QElapsedTimer>
#include <QEventLoop>
#include <QMutexLocker>
#include <QDebug>
#pragma execution_character_set("utf-8")
#include "baserequester.h"

class TcpWrap : public BaseRequester
{
    Q_OBJECT

public:
    explicit TcpWrap(QObject *parent = nullptr);

    void setTcpSocket(QTcpSocket *socket);
    void setKeepLiveData(QByteArray arr, int intervalMs);
    void startKeepLive();
    void stopKeepLive();

    // BaseRequester interface
    virtual bool requestNoReply(QByteArray data) override;
    virtual bool request(QByteArray data, QByteArray &rec) override;

signals:
    void receivedData(QByteArray data);

private slots:
    void onError(QAbstractSocket::SocketError socketError);
    void onReadyRead();

private:
    void waitMs(int ms);

    QTcpSocket *_tcpSocket = nullptr;
    QTimer *_timer = nullptr;
    QByteArray _keepLiveData;
    QList<QByteArray> _recList;
    QMutex _mutex;

};

#endif // TCPWRAP_H
